import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Scatter, Map, Pie, Grid
from pyecharts.commons.utils import JsCode

# 加载数据
data = pd.read_excel('top10.xlsx')

# 创建第一个柱状图
salary = (
    Bar(init_opts=opts.InitOpts(width="900px", height="600px"))
    .add_xaxis(data['company_name'].tolist())
    .add_yaxis("工资", data['salary'].tolist(), bar_width='50%')
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title='成都地区算法岗位工资排名Top10的公司',
            subtitle='叶玉婷 第一张数据分析图',
        ),
        datazoom_opts=opts.DataZoomOpts(type_='slider', range_start=0, range_end=100),
        legend_opts=opts.LegendOpts(is_show=True),
        visualmap_opts=opts.VisualMapOpts(min_=18000, max_=27500, range_color=['blue', 'green', 'red']),
        xaxis_opts=opts.AxisOpts(axislabel_opts={"interval": "0"}),
        yaxis_opts=opts.AxisOpts(axislabel_opts={"interval": "0"}),
    )
    .set_series_opts(
        itemstyle_opts=opts.ItemStyleOpts(color='black'),
        linestyle_opts=opts.LineStyleOpts(width=1, color='red', type_='dotted'),
        label_opts=opts.LabelOpts(position='top'),
        markpoint_opts=opts.MarkPointOpts(data=[
            opts.MarkPointItem(type_='max', symbol='pin', symbol_size=100),
            opts.MarkPointItem(type_='min', symbol='pin', symbol_size=100),
        ]),
        markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_='average')]),
    )
)

# 创建第二个柱状图
categories = ["算法", "图像", "人工智能", "安卓", "Android", "数据", "Java", "前端", "软件", "运维"]
values = [17827, 16994, 16274, 14201, 13910, 13707, 13020, 12365, 11201, 9417]
bar = (
    Bar()
    .add_xaxis(categories)
    .add_yaxis("工资", values, itemstyle_opts=opts.ItemStyleOpts(color=JsCode("""function(params) {var colorList = ['#e6f7ff', '#e9d7fe', '#e3f9e5', '#ffc107', '#fde2e4', '#fff5b1', '#e6fff2', '#f4e2d8', '#ffe7ba', '#00bcd4'];return colorList[params.dataIndex % 10];}""")))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="工资表", pos_left="center"),
        legend_opts=opts.LegendOpts(pos_right="right"),
        xaxis_opts=opts.AxisOpts(axislabel_opts={"rotate": 15}),
        yaxis_opts=opts.AxisOpts(name="工资"),
        tooltip_opts=opts.TooltipOpts(is_show=True),
    )
    .set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="inside"))
)

# 创建地图
data_map = [("北京", 18000), ("上海", 17500), ("广东", 17000), ("浙江", 16500), ("江苏", 16000)]
map_chart = (
    Map()
    .add("人工智能岗位平均工资", data_map, "china")
    .set_global_opts(
        title_opts=opts.TitleOpts(title="全国城市人工智能岗位平均工资"),
        visualmap_opts=opts.VisualMapOpts(max_=19000, min_=13000, is_calculable=True),
    )
)

# 创建第四个柱状图
degrees = ["大专", "本科", "硕士", "博士", "2年经验"]
data_chengdu_data = [46, 90, 5, 0, 0]
data_chengdu_algorithm = [0, 120, 85, 5, 1]
bar_chengdu = (
    Bar()
    .add_xaxis(degrees)
    .add_yaxis("数据成都", data_chengdu_data, color="pink")
    .add_yaxis("算法成都", data_chengdu_algorithm, color="skyblue")
    .set_global_opts(
        title_opts=opts.TitleOpts(title="叶玉婷 数据成都和算法成都学历要求"),
        legend_opts=opts.LegendOpts(is_show=True),
        yaxis_opts=opts.AxisOpts(name="人数"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)

# 创建饼图
data_pair = [("前端", 9509), ("软件", 7522), ("运维", 6774), ("Java", 4446), ("Android", 3053), ("数据", 2106), ("算法", 1619), ("安卓", 749), ("图像", 490), ("人工智能", 224)]
pie = (
    Pie()
    .add(series_name="", data_pair=data_pair, radius=["40%", "55%"])
    .set_global_opts(
        title_opts=opts.TitleOpts(title="各类岗位占比", pos_left="center"),
        legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_right="2%"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)"))
    .set_colors(["#4B79A1", "#58D68D", "#F7C46C", "#E14A5B", "#8A2BE2", "#A1C4CF", "#F4A460", "#DA70D6", "#FFC0CB", "#FF69B4"])
)

# 使用Grid布局将图表放入同一个网页中
grid = (
    Grid(init_opts=opts.InitOpts(width="1800px", height="1200px"))
    .add(bar, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right="60%"))
    .add(salary, grid_opts=opts.GridOpts(pos_bottom="60%", pos_left="60%"))
    .add(bar_chengdu, grid_opts=opts.GridOpts(pos_top="60%", pos_right="60%"))
    .add(map_chart, grid_opts=opts.GridOpts(pos_top="60%", pos_left="60%"))
    .add(pie, grid_opts=opts.GridOpts(pos_bottom="60%", pos_left="60%"))
)

# 渲染图表到HTML文件
grid.render("5张图.html")